home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / muds / pennmush.000 / pennmush-1.50-p8-linux.tar / pennmush / conf.c < prev    next >
C/C++ Source or Header  |  1993-04-09  |  10KB  |  346 lines

  1. /* conf.c */
  2.  
  3. /* configuration adjustment. Some of the ideas and bits and pieces of the
  4.  * code here are based on TinyMUSH 2.0.
  5.  */
  6.  
  7. #include <stdio.h>
  8.  
  9. #include "config.h"
  10. #include "interface.h"
  11. #include "db.h"
  12.  
  13. extern object_flag_type find_flag();
  14.  
  15. void cf_str();
  16. void cf_int();
  17. void cf_flag();
  18. void cf_bool();
  19.  
  20. OPTTAB options;
  21.  
  22. typedef struct confparm CONF;
  23.  
  24. struct confparm {
  25.   char *name;            /* name of option */
  26.   void (*handler)();        /* set option with this handler */
  27.   int *loc;            /* place to put this option */
  28.   int max;            /* max: string length, integer value */
  29. };
  30.  
  31. CONF conftable[] = {
  32. {(char *)"mud_name", cf_str, (int *)options.mud_name, 128},
  33. {(char *)"port", cf_int, &options.port, 32000},
  34. {(char *)"input_database", cf_str, (int *)options.input_db, 256},
  35. {(char *)"output_database", cf_str, (int *)options.output_db, 256},
  36. {(char *)"crash_database", cf_str, (int *)options.crash_db, 256},
  37. {(char *)"mail_database", cf_str, (int *)options.mail_db, 256},
  38. {(char *)"guest_player", cf_int, &options.guest_player, 100000},
  39. {(char *)"player_start", cf_int, &options.player_start, 100000},
  40. {(char *)"master_room", cf_int, &options.master_room, 100000},
  41. {(char *)"idle_timeout", cf_int, &options.idle_timeout, 100000},
  42. {(char *)"dump_interval", cf_int, &options.dump_interval, 100000},
  43. {(char *)"max_logins", cf_int, &options.max_logins, 128},
  44. {(char *)"paycheck", cf_int, &options.paycheck, 1000},
  45. {(char *)"starting_money", cf_int, &options.starting_money, 10000},
  46. {(char *)"player_queue_limit", cf_int, &options.player_queue_limit, 100000},
  47. {(char *)"money_singular", cf_str, (int *)options.money_singular, 32},
  48. {(char *)"money_plural", cf_str, (int *)options.money_plural, 32},
  49. {(char *)"compress_program", cf_str, (int *)options.compress, 256},
  50. {(char *)"uncompress_program", cf_str, (int *)options.uncompress, 256},
  51. {(char *)"help_file", cf_str, (int *)options.help_file, 256},
  52. {(char *)"help_index", cf_str, (int *)options.help_index, 256},
  53. {(char *)"news_file", cf_str, (int *)options.news_file, 256},
  54. {(char *)"news_index", cf_str, (int *)options.news_index, 256},
  55. {(char *)"events_file", cf_str, (int *)options.events_file, 256},
  56. {(char *)"events_index", cf_str, (int *)options.events_index, 256},
  57. {(char *)"connect_file", cf_str, (int *)options.connect_file, 256},
  58. {(char *)"motd_file", cf_str, (int *)options.motd_file, 256},
  59. {(char *)"wizmotd_file", cf_str, (int *)options.wizmotd_file, 256},
  60. {(char *)"newuser_file", cf_str, (int *)options.newuser_file, 256},
  61. {(char *)"register_create_file", cf_str, (int *)options.register_file, 256},
  62. {(char *)"quit_file", cf_str, (int *)options.quit_file, 256},
  63. {(char *)"down_file", cf_str, (int *)options.down_file, 256},
  64. {(char *)"full_file", cf_str, (int *)options.full_file, 256},
  65. {(char *)"log_commands", cf_bool, &options.log_commands, 2},
  66. {(char *)"log_huhs", cf_bool, &options.log_huhs, 2},
  67. {(char *)"log_forces", cf_bool, &options.log_forces, 2},
  68. {(char *)"log_walls", cf_bool, &options.log_walls, 2},
  69. {(char *)"logins", cf_bool, &options.login_allow, 2},
  70. {(char *)"daytime", cf_bool, &options.daytime, 2},
  71. {(char *)"player_flags", cf_flag, &options.player_flags, 64},
  72. {(char *)"room_flags", cf_flag, &options.room_flags, 64},
  73. {(char *)"exit_flags", cf_flag, &options.exit_flags, 64},
  74. {(char *)"thing_flags", cf_flag, &options.thing_flags, 64},
  75. {(char *)"rwho_dump_interval", cf_int, &options.rwho_interval, 32000},
  76. {(char *)"rwho_info_port", cf_int, &options.rwho_port, 32000},
  77. {(char *)"rwho_host", cf_str, (int *)options.rwho_host, 64},
  78. {(char *)"rwho_password", cf_str, (int *)options.rwho_pass, 64},
  79. { NULL, NULL, NULL, 0}
  80. };
  81.  
  82. void cf_bool(opt, val, loc, maxval)
  83.      char *opt;
  84.      char *val;
  85.      int *loc;
  86.      int maxval;
  87. {
  88.   /* enter boolean parameter */
  89.  
  90.   if (!strcasecmp(val, "yes") || !strcasecmp(val, "true") ||
  91.       !strcasecmp(val, "1"))
  92.     *loc = 1;
  93.   else if (!strcasecmp(val, "no") || !strcasecmp(val, "false") ||
  94.        !strcasecmp(val, "0"))
  95.     *loc = 0;
  96.   else {
  97.     fprintf(stderr, "CONFIGURATION: option %s value %s invalid.\n", opt, val);
  98.     fflush(stderr);
  99.   }
  100. }
  101.  
  102. void cf_str(opt, val, loc, maxlen)
  103.      char *opt;
  104.      char *val;
  105.      int *loc;
  106.      int maxlen;
  107. {
  108.   /* enter string parameter */
  109.  
  110.   /* truncate if necessary */
  111.   if (strlen(val) >= maxlen) {
  112.     val[maxlen - 1] = '\0';
  113.     fprintf(stderr, "CONFIGURATION: option %s value truncated\n", opt);
  114.     fflush(stderr);
  115.   }
  116.  
  117.   strcpy((char *)loc, val);
  118. }
  119.     
  120. void cf_int(opt, val, loc, maxval)
  121.      char *opt;
  122.      char *val;
  123.      int *loc;
  124.      int maxval;
  125. {
  126.   /* enter integer parameter */
  127.   
  128.   int n;
  129.  
  130.   n = atoi(val);
  131.  
  132.   /* enforce limits */
  133.   if (n > maxval) {
  134.     n = maxval;
  135.     fprintf(stderr, "CONFIGURATION: option %s value limited to %d\n", 
  136.         opt, maxval);
  137.     fflush(stderr);
  138.   }
  139.  
  140.   *loc = n;
  141. }
  142.  
  143. void cf_flag(opt, val, loc, maxval)
  144.      char *opt;
  145.      char *val;
  146.      int *loc;
  147.      int maxval;
  148. {
  149.   /* set default flags */
  150.  
  151.   int f = -1;
  152.   int toggle;
  153.  
  154.   /* figure out what flag type we're setting */
  155.  
  156.   switch (opt[0]) {
  157.   case 'p':
  158.     f = find_flag(val, TYPE_PLAYER, &toggle, 1);
  159.     break;
  160.   case 'r':
  161.     f = find_flag(val, TYPE_ROOM, &toggle, 1);
  162.     break;
  163.   case 'e':
  164.     f = find_flag(val, TYPE_EXIT, &toggle, 1);
  165.     break;
  166.   case 't':
  167.     f = find_flag(val, TYPE_THING, &toggle, 1);
  168.     break;
  169.   default:
  170.     fprintf(stderr, "CONFIGURATION: weird flag set directive '%s'\n", opt);
  171.   }
  172.  
  173.   if (f == -1) {
  174.     fprintf(stderr, "CONFIGURATION: flag '%s' cannot be set.\n", val);
  175.     return;
  176.   }
  177.  
  178.   if (f == -2) {
  179.     fprintf(stderr, "CONFIGURATION: flag '%s' for type not found.\n", val);
  180.     return;
  181.   }
  182.   
  183.   if (!toggle)
  184.     *loc |= f;
  185.   else {
  186.     switch (opt[0]) {
  187.     case 'p':
  188.       options.player_toggles |= f;
  189.       break;
  190.     case 'r':
  191.       options.room_toggles |= f;
  192.       break;
  193.     case 'e':
  194.       options.exit_toggles |= f;
  195.       break;
  196.     case 't':
  197.       options.thing_toggles |= f;
  198.       break;
  199.     }
  200.   }
  201. }
  202.  
  203. void config_set(opt, val)
  204.      char *opt;
  205.      char *val;
  206. {
  207.   CONF *cp;
  208.  
  209.   /* search conf table for the option; if found, add it, if not found,
  210.    * complain about it.
  211.    */
  212.  
  213.   for (cp = conftable; cp->name; cp++) {
  214.     if (!strcmp(cp->name, opt)) {
  215.       cp->handler(opt, val, cp->loc, cp->max);
  216.       return;
  217.     }
  218.   }
  219.  
  220.   fprintf(stderr, "CONFIGURATION: directive '%s' not found.\n", opt);
  221.   fflush(stderr);
  222. }
  223.  
  224. void conf_default_set()
  225. {
  226.     strcpy(options.mud_name, "TinyMUSH");
  227.     options.port = 4201;
  228.     strcpy(options.input_db, "data/indb.Z");
  229.     strcpy(options.output_db, "data/outdb.Z");
  230.     strcpy(options.crash_db, "data/PANIC.db");
  231.     strcpy(options.mail_db, "maildb.Z");
  232.     options.guest_player = -1;
  233.     options.player_start = 0;
  234.     options.master_room = 2;
  235.     options.idle_timeout = 10801;
  236.     options.dump_interval = 3601;
  237.     options.max_logins = 128;
  238.     options.paycheck = 50;
  239.     options.starting_money = 100;
  240.     options.player_queue_limit = 100;
  241.     strcpy(options.money_singular, "Penny");
  242.     strcpy(options.money_plural, "Pennies");
  243.     strcpy(options.compress, "compress");
  244.     strcpy(options.uncompress, "uncompress");
  245.     strcpy(options.help_file, "txt/help.txt");
  246.     strcpy(options.help_index, "txt/help.indx");
  247.     strcpy(options.news_file, "txt/news.txt");
  248.     strcpy(options.news_index, "txt/news.indx");
  249.     strcpy(options.events_file, "txt/events.txt");
  250.     strcpy(options.events_index, "txt/events.indx");
  251.     strcpy(options.connect_file, "txt/connect.txt");
  252.     strcpy(options.motd_file, "txt/motd.txt");
  253.     strcpy(options.wizmotd_file, "txt/wizmotd.txt");
  254.     strcpy(options.newuser_file, "txt/newuser.txt");
  255.     strcpy(options.register_file, "txt/create_reg.txt");
  256.     strcpy(options.quit_file, "txt/quit.txt");
  257.     strcpy(options.down_file, "txt/down.txt");
  258.     strcpy(options.full_file, "txt/full.txt");
  259.     options.log_commands = 0;
  260.     options.log_huhs = 0;
  261.     options.log_forces = 1;
  262.     options.log_walls = 0;
  263.     options.login_allow = 1;
  264.     options.daytime = 0;
  265.     options.player_flags = 0;
  266.     options.room_flags = 0;
  267.     options.exit_flags = 0;
  268.     options.thing_flags = 0;
  269.     options.player_toggles = 0;
  270.     options.room_toggles = 0;
  271.     options.exit_toggles = 0;
  272.     options.thing_toggles = 0;
  273.     options.rwho_interval = 241;
  274.     options.rwho_port = 6889;
  275.     strcpy(options.rwho_host, "riemann.math.okstate.edu");
  276.     strcpy(options.rwho_pass, "getyours");
  277. }
  278.  
  279. int config_file_startup(conf)
  280.      char *conf;
  281. {
  282.   /* read a configuration file. Return 0 on failure, 1 on success */
  283.   
  284.   FILE *fp;
  285.   char tbuf1[BUFFER_LEN];
  286.   char *p, *q, *s;
  287.   
  288.   fp = fopen(conf, "r");
  289.   if (fp == NULL) {
  290.     fprintf(stderr, "ERROR: Cannot open configuration file %s.\n", conf);
  291.     return 0;
  292.   }
  293.  
  294.   conf_default_set();             /* initialize defaults */
  295.  
  296.   fgets(tbuf1, BUFFER_LEN, fp);
  297.   while (!feof(fp)) {
  298.  
  299.     p = tbuf1;
  300.  
  301.     if (*p == '#') {
  302.       /* comment line */
  303.       fgets(tbuf1, BUFFER_LEN, fp);
  304.       continue;
  305.     }
  306.  
  307.     /* this is a real line. Strip the newline and characters following it.
  308.      * Split the line into command and argument portions. If it exists,
  309.      * also strip off the trailing comment.
  310.      */
  311.  
  312.     for (p = tbuf1; *p && (*p != '\n'); p++)
  313.       ;
  314.     *p = '\0';                                /* strip '\n' */
  315.     for (p = tbuf1; *p && isspace(*p); p++)         /* strip spaces */
  316.       ;
  317.     for (q = p; *q && !isspace(*q); q++)            /* move over command */
  318.       ;
  319.     if (*q)
  320.       *q++ = '\0';                            /* split off command */
  321.     for (; *q && isspace(*q); q++)                  /* skip spaces */
  322.       ;
  323.     for (s = q; *s && (*s != '#'); s++)                /* look for comment */
  324.       ;
  325.     if (*s)                                /* if found nuke it */
  326.       *s = '\0';
  327.     for (s = s - 1; (s >= q) && isspace(*s); s--)   /* smash trailing stuff */
  328.       *s = '\0';
  329.  
  330.     if (strlen(p) != 0)        /* skip blank lines */
  331.       config_set(p, q);
  332.  
  333.     fgets(tbuf1, BUFFER_LEN, fp);
  334.   }
  335.  
  336.   /* these directives aren't player-settable but need to be initialized */
  337.   options.dump_counter = options.dump_interval;
  338. #ifdef RWHO_SEND
  339.   options.rwho_counter = options.rwho_interval;
  340. #endif
  341.  
  342.   fclose(fp);
  343.   return 1;
  344. }
  345.  
  346.